package com.hejia.alauda.entity;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.hejia.alauda.Constants;

/**
 * 订单记录(侧重交易过程) Created by Johnny(johnny@hejiajinrong.com) on 14-8-29.
 */
public class Order extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// STATUS_OVERDUE-到期未付的，拖欠的订单, STATUS_UNPAY-购买产品后暂时未付款,
	// STATUS_PAID-购买产品后已经付款,STATUS_FAILED-支付失败，STATUS_EXCESS超额支付
	// STATUS_CONFIRM 订单已支付正在等待银行确认
	public enum Status {
		STATUS_OVERDUE, STATUS_UNPAY, STATUS_PAID, STATUS_FAILED, STATUS_EXCESS, STATUS_CONFIRM, STATUS_PAYING/*
																											 * 正在支付
																											 * 暂时备用
																											 */;
	}

	public enum Source {
		IOS, Android, Web, H5
	}

	/**
	 * 订单过期时间，单位分钟
	 */
	public static final Integer EXPIRE_TIME = 10;

	/**
	 * 订单号前缀
	 */
	public static final String ORDER_NUMBER_PREFIX = "DD";
	
	@Column
	private String orderNumber;// 订单号

	@Column(length = 32)
	private String payNumber;// 支付订单号

	@Column
	private Product product;// 产品

	@Column
	private User user;// 用户

	@Column
	private BigDecimal amount; // 购买金额

	@Column
	private String payGateCode;// 支付网关代码
	
	@Column
	private String payGateStatus; // 从支付网关查询到的订单支付状态
	
	@Column
	private String payGateStatusDescription; // 从支付网关查询到的订单支付状态描述

	@Column
	private Status status;

	@Column
	private Payment payment;

	@Column
	private BankCard bankcard;

	@Column
	private String tag;

	@Column(length = 10)
	private Source source;// 订单来源

	private Date dueDate;// 到期日
	
	@Column
	private Date refundDate; // 退款日期
	
	@Column
	private String track;

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public String getPayNumber() {
		return payNumber;
	}

	public void setPayNumber(String payNumber) {
		this.payNumber = payNumber;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public Payment getPayment() {
		return payment;
	}

	public void setPayment(Payment payment) {
		this.payment = payment;
	}

	public String getStatusStr() {
		String statusStr = "未知状态";
		if (getStatus() != null) {
			switch (getStatus()) {
			case STATUS_OVERDUE:
				statusStr = "订单失效";
				break;
			case STATUS_UNPAY:
				statusStr = "未付款";
				break;
			case STATUS_PAID:
				statusStr = "交易成功";
				break;
			case STATUS_FAILED:
				statusStr = "交易失败";
				break;
			case STATUS_EXCESS:
				statusStr = "超额支付";
				break;
			case STATUS_CONFIRM:
				statusStr = "确认中";
				break;
			default:
				break;
			}
		}
		return statusStr;
	}

	public BankCard getBankcard() {
		return bankcard;
	}

	public void setBankcard(BankCard bankcard) {
		this.bankcard = bankcard;
	}

	public Source getSource() {
		return source;
	}

	public void setSource(Source source) {
		this.source = source;
	}

	public void setSource(String source) {
		if ("IOS".equalsIgnoreCase(source)) {
			this.source = Source.IOS;
		} else if ("Android".equalsIgnoreCase(source)) {
			this.source = Source.Android;
		} else if ("H5".equalsIgnoreCase(source)) {
			this.source = Source.H5;
		} else {
			this.source = Source.Web;
		}
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getPayGateCode() {
		return payGateCode;
	}

	public void setPayGateCode(String payGateCode) {
		this.payGateCode = payGateCode;
	}

	/**
	 * 获取支付网关名称
	 * 
	 * @return 支付网关名称
	 */
	public String getPayGateName() {

		if (StringUtils.isEmpty(this.payGateCode)) {
			return "连连支付";
		}

		String name = "";
		switch (this.payGateCode) {
		case Constants.PAYGATE_CODE_LLPAY:
			name = "连连支付";
			break;
		case Constants.PAYGATE_CODE_YEEPAY:
			name = "易宝支付";
			break;
		case Constants.PAYGATE_CODE_PAYECO:
			name = "易联支付";
			break;
		default:
			name = this.payGateCode;
			break;
		}
		return name;
	}
	
	public String getPayGateStatus() {
		return payGateStatus;
	}

	public void setPayGateStatus(String payGateStatus) {
		this.payGateStatus = payGateStatus;
	}
	
	public String getPayGateStatusDescription() {
		return payGateStatusDescription;
	}

	public void setPayGateStatusDescription(String payGateStatusDescription) {
		this.payGateStatusDescription = payGateStatusDescription;
	}

	public Date getDueDate() {
		
		if (dueDate == null 
		&& getCreateDate() != null 
		&& getProduct() != null) {
			dueDate = new DateTime(getCreateDate())
				.plusDays(getProduct().getTerm() - 1)
				.toDate();
		}
		
		return dueDate;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	public Date getRefundDate() {
		return refundDate;
	}

	public void setRefundDate(Date refundDate) {
		this.refundDate = refundDate;
	}

	public String getTrack() {
		return track;
	}

	public void setTrack(String track) {
		this.track = track;
	}

	
}
